class_name PlayerStatusBase extends StatusBase

# 由父节点 status machine指定
var actor:Player

############################ start 速度处理
func change_to_jump()->bool:
	actor.velocity.y = blackboard.JUMP_VELOCITY
	return change_status("Jump")
	
func change_to_walljump(jump_direction:int)->bool:
	actor.velocity.y = blackboard.JUMP_VELOCITY * 1.2
	actor.velocity.x = jump_direction * blackboard.SPEED
	return change_status("WallJump")

# 用户控制方向
func common_velocity(delta:float)->void:
	move_y(delta)
	if actor.is_on_floor():
		# 地面加速度
		move_toward_x(5*delta)
	else:
		# 空中加速度变大:即空中转身更加灵活
		move_toward_x(10*delta)
		
	actor.refresh_face_direction()
	
# 击退处理
func hurt_velocity(delta:float)->void:
	# 击退方向
	var pending_damage = blackboard.get_damage()
	if pending_damage != null:
		_repel()
	else:
		move_y(delta)
		move_toward_x(2*delta, 0.0)	

# 僵直速度
func stand_velocity(delta:float)->void:
	move_y(delta)
	move_toward_x(5*delta, 0.0)

# 滑动速度
func sliding_stand_velocity(delta:float)->void:
	stand_velocity(delta)

# 击退
func _repel():
	var pending_damage = blackboard.get_damage()
	var dir = pending_damage.source.global_position.direction_to(actor.global_position)
	if dir.x > 0:
		actor.face_direction(-1)
	elif dir.x < 0:
		actor.face_direction(1)
	actor.velocity = dir * 200
	if actor.velocity.y > -50:
		actor.velocity.y = blackboard.JUMP_VELOCITY / 2.0
		
	blackboard.free_damage()
	
func move_toward_x(accelerate:float, speed_scale:float=1.0, direction:int=0):
	if direction == 0 && speed_scale != 0.0:
		direction = actor.get_direction()
	actor.velocity.x = move_toward(actor.velocity.x, direction * blackboard.SPEED * speed_scale, blackboard.SPEED * accelerate )

func move_y(delta:float):
	if not actor.is_on_floor():
		actor.velocity.y += actor.gravity * delta

############################ end 速度处理
